home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TRANS-LSC / MSkelHelp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-12  |  6.5 KB  |  264 lines  |  [TEXT/KAHL]

  1. /*
  2.     TransSkel multiple-window demonstration: Help module
  3.  
  4.     This module handles a help window, in which text may be scrolled but
  5.     not edited.  A TextEdit record is used to hold the text, though.
  6.  
  7.     14 June 1986        Paul DuBois
  8. */
  9.  
  10. # include    "MultiSkel.h"
  11. # include    <ControlMgr.h>
  12. # include    <TextEdit.h>
  13.  
  14.  
  15.  
  16.  
  17. WindowPtr                helpWind;
  18. static TEHandle            teHelp;        /* handle to help window TextEdit record */
  19. static ControlHandle    helpScroll;    /* help window scroll bar */
  20. static int                helpLine;    /* line currently at top of window */
  21. static int                halfPage;    /* number of lines in half a window */
  22.  
  23.  
  24. static Halt ()
  25. {
  26.     TEDispose (teHelp);
  27.     DisposeControl (helpScroll);
  28.     CloseWindow (helpWind);
  29. }
  30.  
  31.  
  32. /*
  33.     Scroll to the correct position.  lDelta is the
  34.     amount to CHANGE the current scroll setting by.
  35. */
  36.  
  37. DoScroll (lDelta)
  38. int        lDelta;
  39. {
  40. int        newLine;
  41.  
  42.     newLine = helpLine + lDelta;
  43.     if (newLine < 0)
  44.         newLine = 0;
  45.     if (newLine > GetCtlMax (helpScroll))
  46.         newLine = GetCtlMax (helpScroll);
  47.     SetCtlValue (helpScroll, newLine);
  48.     lDelta = (helpLine - newLine ) * (**teHelp).lineHeight;
  49.     TEScroll (0, lDelta, teHelp);
  50.     helpLine = newLine;
  51. }
  52.  
  53.  
  54. /*
  55.     Filter proc for tracking mousedown in scroll bar.  The part code
  56.     of the part originally hit is stored as the control's reference
  57.     value.
  58.  
  59.     The "void" had better be there!  Otherwise Lightspeed will treat
  60.     it as an integer function, not a procedure.
  61. */
  62.  
  63. pascal void TrackScroll (theScroll, partCode)
  64. ControlHandle    theScroll;
  65. int                partCode;
  66. {
  67. int                lDelta;
  68.  
  69.     if (partCode == GetCRefCon (theScroll))    /* still in same part? */
  70.     {
  71.         switch (partCode)
  72.         {
  73.             case inUpButton: lDelta = -1; break;
  74.             case inDownButton: lDelta = 1; break;
  75.             case inPageUp: lDelta = -halfPage; break;
  76.             case inPageDown: lDelta = halfPage; break;
  77.         }
  78.         DoScroll (lDelta);
  79.     }
  80. }
  81.  
  82.  
  83. /*
  84.     Handle hits in scroll bar
  85. */
  86.  
  87. static Mouse (thePt, t, mods)
  88. Point    thePt;
  89. long    t;
  90. int        mods;
  91. {
  92. int        thePart;
  93.  
  94.         if ((thePart = TestControl (helpScroll, thePt)) == inThumb)
  95.         {
  96.             (void) TrackControl (helpScroll, thePt, nil);
  97.             DoScroll (GetCtlValue (helpScroll) - helpLine);
  98.         }
  99.         else if (thePart != 0)
  100.         {
  101.             SetCRefCon (helpScroll, (long) thePart);
  102.             (void) TrackControl (helpScroll, thePt, &TrackScroll);
  103.         }
  104. }
  105.  
  106.  
  107. /*
  108.     Update help window.  The update event might be in response to a
  109.     window resizing.  If so, resize the rects and recalc the linestarts
  110.     of the text.  To resize the rects, only the right edge of the
  111.     destRect need be changed (the bottom is not used, and the left and
  112.     top should not be changed). The viewRect should be sized to the
  113.     screen.  Pull text down if necessary to fill window.
  114. */
  115.  
  116. static Update (resized)
  117. Boolean    resized;
  118. {
  119. Rect    r;
  120. int        visLines;
  121. int        lHeight;
  122. int        topLines;
  123. int        nLines;
  124. int        scrollLines;
  125.  
  126.     r = helpWind->portRect;
  127.     EraseRect (&r);
  128.     if (resized)
  129.     {
  130.         r.left += 4;
  131.         r.bottom -= 2;
  132.         r.top += 2;
  133.         r.right -= 19;
  134.         (**teHelp).destRect.right = r.right;
  135.         (**teHelp).viewRect = r;
  136.         TECalText (teHelp);
  137.         lHeight = (**teHelp).lineHeight;
  138.         nLines = (**teHelp).nLines;
  139.         visLines = (r.bottom - r.top) / lHeight;
  140.         halfPage = visLines / 2;
  141.         topLines = (r.top - (**teHelp).destRect.top) / lHeight;
  142.         scrollLines = visLines - (nLines - topLines);
  143.         if (scrollLines > 0 && topLines > 0)
  144.         {
  145.             if (scrollLines > topLines)
  146.                 scrollLines = topLines;
  147.             TEScroll (0, scrollLines * lHeight, teHelp);
  148.         }
  149.         scrollLines = nLines - visLines;
  150.         helpLine = (r.top - (**teHelp).destRect.top) / lHeight;
  151. /*
  152.     move and resize the scroll bar as well.  The ValidRect call is done
  153.     because the HideControl adds the control bounds box to the update
  154.     region - which would generate another update event!  Since everything
  155.     gets redrawn below, the ValidRect is used to cancel the update.
  156. */
  157.  
  158.         HideControl (helpScroll);
  159.         r = helpWind->portRect;
  160.         r.left = r.right - 15;
  161.         r.bottom -= 14;
  162.         --r.top;
  163.         ++r.right;
  164.         SizeControl (helpScroll, r.right - r.left, r.bottom - r.top);
  165.         MoveControl (helpScroll, r.left, r.top);
  166.         SetCtlMax (helpScroll, nLines - visLines < 0 ? 0 : nLines - visLines);
  167.         SetCtlValue (helpScroll, helpLine);
  168.         /*if (scrollLines <= 0)
  169.             HiliteControl (helpScroll, (scrollLines > 0 ? 0 : 255));*/
  170.         ShowControl (helpScroll);
  171.         /*if (GetCtlValue (helpScroll) > scrollLines)
  172.             DoScroll (GetCtlValue (helpScroll) - scrollLines);*/
  173.     }
  174.     DrawGrowBox (helpWind);
  175.     DrawControls (helpWind);    /* redraw scroll bar */
  176.     r = (**teHelp).viewRect;
  177.     TEUpdate (&r, teHelp);        /* redraw text display */
  178.     ValidRect (&helpWind->portRect);
  179. }
  180.  
  181.  
  182. /*
  183.     When the window comes active, disable the Edit menu and highlight
  184.     the scroll bar if there are any lines not visible in the content
  185.     region.  When the window is deactivated, enable the Edit menu and
  186.     un-highlight the scroll bar.
  187. */
  188.  
  189. static Activate (active)
  190. Boolean    active;
  191. {
  192.     DrawGrowBox (helpWind);
  193.     if (active)
  194.     {
  195.         DisableItem (editMenu, 0);
  196.         HiliteControl (helpScroll, (GetCtlMax (helpScroll) > 0 ? 0 : 255));
  197.     }
  198.     else
  199.     {
  200.         EnableItem (editMenu, 0);
  201.         HiliteControl (helpScroll, 255);
  202.     }
  203.     DrawMenuBar ();
  204. }
  205.  
  206.  
  207. HelpWindInit ()
  208. {
  209. Rect    r;
  210. Handle    textHandle;
  211. int        visLines;
  212. int        scrollLines;
  213.  
  214.     helpWind = GetNewWindow (helpWindRes, nil, -1L);
  215.     SkelWindow (helpWind, Mouse, nil, Update,
  216.                 Activate, nil, Halt, nil, true);
  217.  
  218.     TextFont (0);
  219.     TextSize (0);
  220.  
  221.     r = helpWind->portRect;
  222.     r.left += 4;
  223.     r.bottom -= 2;
  224.     r.top += 2;
  225.     r.right -= 19;
  226.     teHelp = TENew (&r, &r);
  227.     textHandle = GetResource ('TEXT', helpTextRes);    /* read help text */
  228.     HLock (textHandle);        /* lock it and insert into TERec */
  229.     TEInsert (*textHandle, GetHandleSize (textHandle), teHelp);
  230.     HUnlock (textHandle);
  231.     ReleaseResource (textHandle);    /* done with it, so goodbye */
  232. /*
  233.     Now figure out how many lines will fit in the window and how many
  234.     will not.  Determine the number of lines in half a window for use
  235.     in tracking clicks in the page up and page down regions of the
  236.     scroll bar.  Then create the scroll bar .  Make sure the borders
  237.     overlap the window frame and the frame of the grow box.
  238. */
  239.     visLines = (r.bottom - r.top) / (**teHelp).lineHeight;
  240.     scrollLines = (**teHelp).nLines - visLines;
  241.     halfPage = visLines / 2;
  242.     helpLine = 0;
  243.     r = helpWind->portRect;
  244.     r.left = r.right - 15;
  245.     r.bottom -= 14;
  246.     --r.top;
  247.     ++r.right;
  248. /*
  249.     Build the scroll bar.  Don't need to bother testing whether to
  250.     highlight it or not, since that will be done in response to the
  251.     activate event.
  252. */
  253.     helpScroll = NewControl (helpWind, &r, "\p", true,
  254.                     helpLine, 0, scrollLines, scrollBarProc, 0L);
  255.  
  256. /*
  257.     GetNewWindow generates an update event for entire portRect.
  258.     Cancel it, since the everything has been drawn already,
  259.     except for the grow box (which will be drawn in response
  260.     to the activate event).
  261. */
  262.     ValidRect (&helpWind->portRect);
  263. }
  264.